package com.example.network.demos.controller;

import cn.hutool.core.io.FileUtil;
import com.example.network.demos.config.NetworkConfig;
//import com.example.network.demos.tcp.client.dongzong.NettyTcpClientDongzong;
//import com.example.network.demos.tcp.client.fuzou.NettyTcpClientFuzou;
import com.example.network.demos.tcp.client.dongzong.NettyTcpClientDongzong;
import com.example.network.demos.tcp.client.fuzou.NettyTcpClientFuzou;
import com.example.network.demos.tcp.client.nanchu.NettyTcpClientNanchu;
import com.example.network.demos.tcp.client.wuxi.NettyTcpClientWuxi;
import com.example.network.demos.tcp.domain.TCPMessage;
import com.example.network.demos.tcp.utils.DockingUtils;
import com.example.network.demos.tcp.utils.ParseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Objects;

@RestController
@RequestMapping("/tcp")
public class TCPController {

    @Autowired
    private NettyTcpClientWuxi nettyTcpClientWuxi;
    @Autowired
    private NettyTcpClientDongzong nettyTcpClientDongzong;
    @Autowired
    private NettyTcpClientNanchu nettyTcpClientNanchu;
    @Autowired
    private NettyTcpClientFuzou nettyTcpClientFuzou;

    @PostMapping("/getmessage")
    public TCPMessage getMessage(@RequestBody TCPMessage message) {
        String campCode = message.getCampCode();
        if(Objects.isNull(campCode)||"".equals(campCode)){
            campCode = NetworkConfig.getCampCodeWuxi();
        }
        if(NetworkConfig.getCampCodeWuxi().equals(campCode)){
            TCPMessage result = nettyTcpClientWuxi.getMessage(message);
            return result;
        }

        if(NetworkConfig.getCampCodeDongzong().equals(campCode)){
            TCPMessage result = nettyTcpClientDongzong.getMessage(message);
            return result;
        }

        if(NetworkConfig.getCampCodeNanchu().equals(campCode)){
            TCPMessage result = nettyTcpClientNanchu.getMessage(message);
            return result;
        }

        if(NetworkConfig.getCampCodeFuzou().equals(campCode)){
            TCPMessage result = nettyTcpClientFuzou.getMessage(message);
            return result;
        }

        return new TCPMessage();
    }


    @PostMapping("/sendmessage")
    public TCPMessage sendMessage(@RequestBody TCPMessage message) {

        String campCode = message.getCampCode();
        if(Objects.isNull(campCode)||"".equals(campCode)){
            campCode = NetworkConfig.getCampCodeWuxi();
        }
        if(NetworkConfig.getCampCodeWuxi().equals(campCode)){
            nettyTcpClientWuxi.sendMessage(message);
            return new TCPMessage();
        }

        if(NetworkConfig.getCampCodeDongzong().equals(campCode)){
            nettyTcpClientDongzong.sendMessage(message);
            return new TCPMessage();
        }

        if(NetworkConfig.getCampCodeNanchu().equals(campCode)){
            nettyTcpClientNanchu.sendMessage(message);
            return new TCPMessage();
        }

        if(NetworkConfig.getCampCodeFuzou().equals(campCode)){
            nettyTcpClientFuzou.sendMessage(message);
            return new TCPMessage();
        }

        return new TCPMessage();
    }

    @PostMapping("/tcpsend")
    public TCPMessage tcpsend(@RequestBody TCPMessage message) {
        DockingUtils.postForTCP(message);
        return message;
    }

    /**
     * 删除文件路径
     * @param message
     * @return
     */
    @PostMapping("/rmHlsFile")
    public TCPMessage rmHlsFile(@RequestBody TCPMessage message) {
        String path = (String) message.getData();
        File filePath = new File(NetworkConfig.getProfile()+path);
        if(filePath.exists()){
            FileUtil.del(filePath);
            if(Objects.isNull(message.getCampCode())){
                nettyTcpClientWuxi.sendMessage(TCPMessage.builder()
                        .data("-1")
                        .flag("stopCameraPreviewUrlVideo")
                        .campCode(NetworkConfig.getCampCodeWuxi())
                        .build());
                nettyTcpClientDongzong.sendMessage(TCPMessage.builder()
                        .data("-1")
                        .flag("stopCameraPreviewUrlVideo")
                        .campCode(NetworkConfig.getCampCodeDongzong())
                        .build());
                nettyTcpClientNanchu.sendMessage(TCPMessage.builder()
                        .data("-1")
                        .flag("stopCameraPreviewUrlVideo")
                        .campCode(NetworkConfig.getCampCodeNanchu())
                        .build());
                nettyTcpClientFuzou.sendMessage(TCPMessage.builder()
                        .data("-1")
                        .flag("stopCameraPreviewUrlVideo")
                        .campCode(NetworkConfig.getCampCodeFuzou())
                        .build());
            }

        }
        return message;
    }
}
